const util = require("util");
const fs = require("fs");
const path = require("path");

const filePath = path.resolve(__dirname, "../scripts/t1.txt");

fs.readFile(filePath, "utf8", function (err, result) {
  console.log(2)
  console.error("Error: ", err);
  console.log("Result: ", result); // Nodejs Callback 转 Promise 对象测试
});

const promisifyReadFile = util.promisify(fs.readFile);

promisifyReadFile(filePath).then((data) => {
  console.log("1", data.toString());
});

// 这里可以看到 promise 的 then 的执行在事件循环中要优先于 callback